/** 
 * @author (Gruppe G) 
 * @version (04/18/2012)
 */

package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.LendCtr;
import ModelLayer.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class LendUI
{
    private LendCtr lendCtr;
    
    /**
     * Constructor for objects of class LendUI
     */
    public LendUI()
    {
        lendCtr = new LendCtr();
    }
    
    /**
     * Menu Section
     * 
     * start(), executes the menu.
     * lendMenu(), is a selection method for the user menu.
     * writeLendMenu(), is a visual interpretation of the user menu for the loan section.
     */
    public void start()
    {
        lendMenu();
    }
    
    public void lendMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeLendMenu();
            if (choise == 1)
            {
                startFindLend();
            }
            else
            {
                if (choise == 2)
                {
                    startCreateLend();
                }
                else
                {
                    if (choise == 3)
                    {
                        startReturnLend();
                    }
                    else
                    {
                        if (choise == 4)
                        {
                            exit = true;
                        }
                    }
                }
            }     
        }
    }
    
    private int writeLendMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Show lend information");
        System.out.println(" (2) Create new lend");
        System.out.println(" (3) Return lend");
        System.out.println(" (4) Return");
        System.out.print("\n Make your choise: ");
        
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /**
     * User input section
     * 
     * inputDate(), inputs the current date+year.
     * inputPeriod(), inputs the loan period via user interaction.
     * inputStatus(@param), inputs the loan status trough the data parameter.
     * inputPhone(), inputs the loaners phone number via user interaction.
     * inputTitle(), inputs the dvd title via user interaction.
     */
    private String inputDate() 
    {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    private String inputPeriod()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Loan period: ");
        String period = keyboard.nextLine();
        return period;
    }
    
    private String inputStatus(String data)
    {    
        String status = data;
        return status;
    }
    
    private String inputPhone()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    }
    
    private String inputTitle()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    
    /**
     * User interaction section
     * 
     * startCreateLend(), creates the dvd lend after persons phonenumber and dvd is found in there perspectiv arrays.
     * startFindLend(), finds information about persons lend trough there phonenumber via there perspectiv arrays.
     * startReturnLend(), returns lend and changes status to "Returned" if the persons phonenumber and the dvd is found in there perspectiv arrays.
     */
    private void startCreateLend()
    {
        String personPhone = inputPhone();
        
        Person PersonObj = null;
        PersonObj = lendCtr.findPerson(personPhone);
        
        try
        {
            if (PersonObj != null)
            {  
                String dvdTitle = inputTitle();
                
                DVD DvdObj = null;
                DvdObj = lendCtr.findDVD(dvdTitle);
                
                if (DvdObj != null)
                {
                    String date = inputDate();
                    String period = inputPeriod();
                    String status = inputStatus("Is checked out!");
                    lendCtr.createLend(DvdObj, PersonObj, date, period, status);
                }
                else
                {
                System.out.println("\n ** No DVD with that title was found! **");
                Thread.sleep(1500);
                }   
            }
            else
            {
                System.out.println("\n ** No person with that phonenumber was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println("\n ** An error has occurred! **");
        }
    }
    
    private void startFindLend()
    {
        String personPhone = inputPhone();
        
        ArrayList<Lend> foundLends = lendCtr.findAllLends(personPhone);
        
        try
        {
            if (foundLends != null)
            {
                
                int index = 0;
                for(Lend lendObj : foundLends)
                {
                    if (index == 0)
                    {
                        System.out.println(" -- Personal Information --");
                        System.out.println(" " + lendObj.getPerson().getName());
                        System.out.println(" " + lendObj.getPerson().getAddress());
                        System.out.println(" " + lendObj.getPerson().getCity() + ", " + lendObj.getPerson().getPostalCode());
                        System.out.println("\n -- Lends Conducted --");
                    }
                    System.out.println(" Title: " + lendObj.getDVD().getTitle());
                    System.out.println(" Date: " + lendObj.getDate());
                    System.out.println(" Period: " + lendObj.getPeriod() + "\n");
                    
                    index++;
                }
            
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
            }
            else
            {
                System.out.println("\n ** No lend information was found on that phonenumber! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println("\n ** An error has occurred! **");
        }
    }
    
    private void startReturnLend()
    {
        String personPhone = inputPhone();
        String title = inputTitle();
        
        Lend lendObj = null;
        lendObj = lendCtr.ReturnLend(personPhone, title);
        
        try
        {
            if (lendObj != null)
            {
                System.out.println("\n ** The DVD has been returned! **");
                Thread.sleep(1500); 
            }
            else
            {
                System.out.println("\n ** The lend wasn't found, try again please. **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println("\n ** An error has occurred! **");
        }  
    }
}